import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/integrals_detail_dialog.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/integral_detail_vo.dart';
import 'package:xinxuan/vo/integral_list_vo.dart';

class IntegralPageController extends GetxController {

  var integralTypeStr = '全部'.obs;
  int integralType = -1;
  List<Map<String, dynamic>> integralTypeArr = [
    {
      'typeStr': '全部',
      'type': -1
    },
    {
      'typeStr': '每日签到',
      'type': 1
    },
    {
      'typeStr': '赠送积分',
      'type': 2
    },
    {
      'typeStr': '商品消费',
      'type': 3
    },
    {
      'typeStr': '直推积分',
      'type': 4
    },
    {
      'typeStr': '退还积分',
      'type': 5
    }
  ];
  /// 类型下拉选中项
  var typeScrollController = FixedExtentScrollController( initialItem: 0).obs;
  var year = ''.obs;
  var month = ''.obs;

  /// 积分数据
  var integralData = IntegralListVo(
    data: [],
    meta: IntegralMeta(
      stats: IntegralStats(
        available: 0,
        used: 0,
        expired: 0
      ),
      pagination: CommonPagination(
        total: 0,
        currentPage: 1
      )
    )
  ).obs;

  /// 积分详情
  var integralDetailData = IntegralDetailData().obs;

  @override
  void onReady() {
    changeData(DateTime.now());
    getIntegrals();
    super.onReady();
  }

  /// 修改选中日期
  changeData(DateTime value) {
    year.value = '${value.year}';
    if (value.month < 10) {
      month.value = '0${value.month}';
    } else {
      month.value = '${value.month}';
    }
  }

  /// 修改选中类型
  changeTypeIndex(int index) {
    typeScrollController.value = FixedExtentScrollController( initialItem: index);
  }

  /// 确定选中类型
  determineType() {
    integralTypeStr.value = integralTypeArr[typeScrollController.value.initialItem]['typeStr'];
    integralType = integralTypeArr[typeScrollController.value.initialItem]['type'];
    // update();
    getIntegrals();
  }

  /// 获取积分记录列表
  getIntegrals() async {
    integralData.value.meta!.pagination!.currentPage = 1;
    IntegralListVo tempVo = await HttpUtilService.getIntegrals(createdAt: '${year.value}-${month.value}', page: integralData.value.meta!.pagination!.currentPage!, type: integralType);
    integralData.value = tempVo;
    update();
  }

  /// 获取更多积分记录
  onLoadIntegrals() async {
    if (integralData.value.meta!.pagination!.total! <= integralData.value.data!.length) {
      return;
    }
    integralData.value.meta!.pagination!.currentPage = integralData.value.meta!.pagination!.currentPage! + 1;
    IntegralListVo tempVo = await HttpUtilService.getIntegrals(createdAt: '${year.value}-${month.value}', page: integralData.value.meta!.pagination!.currentPage!, type: integralType);
    integralData.value.data!.addAll(tempVo.data!);
    update();
  }

  /// 获取积分详情
  getIntegralsDetail(int id) async {
    IntegralDetailVo integralDetailVo = await HttpUtilService.getIntegralDetail(id: id);
    integralDetailData.value = integralDetailVo.data!;
    Get.dialog(IntegralsDetailDialog(data: integralDetailData.value));
  }
}